package persistencia;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @author jonathan saurez ulloa
 * to do's: add method overload to include server, port and schema in the props file
 
 * se realiza para cada base de datos
 */

public class JdbcConnector  {
// <editor-fold defaultstate="collapsed" desc="Attributes">
    private String JDBCproperties =  "JDBC.properties";//DO NOT CHANGE IT
    /*the properties file should be in the same package as the class who calls it*/
    
    private String DB;
    private String server;
    private String port;
    private String schema; //TABLAS
    private String driver;
    private Properties FILE = new Properties();
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Public Methods">
    /**This method is intended to be used when you want to use the load method
     * and specify a different props file
     * EXAMPLE:
     * JdbcConnector connector = new JdbcConnector();
     * connector.load(propertiesFile, DB, server, port, schema);
     */
    public JdbcConnector() {}

    public JdbcConnector(String DB, String server, String port, String schema){
        this.setDB(DB);
        this.loadFILE();
        this.setDriver(DB);//this method uses info from properties, you should load the prop file before
        this.setServer(server);
        this.setPort(port);
        this.setSchema(schema);
        
    }

    //use this methos to use a different props file
    public boolean load(String propertiesFile, String DB, String server, String port, String schema){
        this.setDB(DB);
        this.setDriver(DB);
        this.setServer(server);
        this.setPort(port);
        this.setSchema(schema);
        return this.loadFILE(propertiesFile);
    }

    /**
     * Returns a connection to the database using the default user in props file
     * @throws java.lang.Exception
     */
    public Connection getConnection() throws Exception{
        Class.forName(this.getDriver());
        return DriverManager.getConnection(this.getConString(),
                this.getFILE().getProperty(this.getDB()+".userName"),
                this.getFILE().getProperty(this.getDB()+".userPass"));
    }

    public Connection getConnection(String user, String pass) throws Exception{
        Class.forName(this.getDriver());
        return DriverManager.getConnection(this.getConString(), user, pass);
    }

    public String getConString(){
        return "jdbc:"+this.getDB()+"://"+this.getServer()
                +":"+this.getPort()+"/"+this.getSchema();
    }

    //use this method when you change the information in the properties file
    public boolean reload(){
        return this.loadFILE();
    }
    //use this method when you change the database and reusing object
    public boolean reload(String DB, String server, String port, String schema){
        this.setDB(DB);
        boolean bool = this.loadFILE();
        this.setDriver(DB);
        this.setServer(server);
        this.setPort(port);
        this.setSchema(schema);
        return bool;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Private Methods">
    private void setDriver(String DB) {
        if(DB.isEmpty()){//default database is mysql

        }else{
             this.driver = this.getFILE().getProperty(this.getDB()+".driver");
        }
    }

    private void setDB(String DB) {
        this.DB = DB;
    }

    private boolean loadFILE(String fileName) {
        try{//this line loads a properties file located in the same package
            if(fileName.isEmpty()){
                this.loadFILE();
            }else{
                this.getFILE().load(this.getClass().getResourceAsStream(fileName));
            }
            return true;
        }catch(Exception e){
            return false;
        }
    }
    private boolean loadFILE() {
        try{//this line loads a properties file located in the same package
            this.getFILE().load(this.getClass().getResourceAsStream(this.getJDBCproperties()));
            return true;
        }catch(Exception e){
            return false;
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Simple Setters and Getters.">
    public String getDriver() {
        return driver;
    }

    public String getDB() {
        return DB;
    }
     
    public String getJDBCproperties() {
        return JDBCproperties;
    }

    private void setJDBCproperties(String JDBCproperties) {
        this.JDBCproperties = JDBCproperties;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public Properties getFILE() {
        return FILE;
    }
// </editor-fold>
}
